//
//  MHFAQViewController.m
//  MHO
//
//  Created by Hien NV on 6/18/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHFAQViewController.h"
#import "Lib.h"
#import "FAQ.h"
#import "MHFAQCell.h"

@interface MHFAQViewController ()
@property (nonatomic, strong) NSMutableArray* data;
@end

@implementation MHFAQViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    static NSString* cellIdentifier = @"MHFAQCell";
    [self.tableView registerNib:[UINib nibWithNibName:@"MHFAQCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
    
    [self.navibarView.leftButton setImage:[UIImage imageNamed:kFileBtnBack] forState:0];
}

- (void)viewDidAppear:(BOOL)animated {
    [self reloadData];
}

- (void) reloadData {
    [Lib showLoadingView];
    
    if (!self.data) {
        self.data = [NSMutableArray array];
    }
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString* url = [NSString stringWithFormat:@"http://mobile.mho.vn/services/Faq?key=%@", kServerKey];
        
        NSString* retVal = [ServiceLib sendGetRequest:url];
        if (retVal) {
            SBJsonParser* parser = [[SBJsonParser alloc] init];
            NSArray* array = [parser objectWithString:retVal];
            if (array && [array isKindOfClass:[NSArray class]]) {
                for (id dict in array) {
                    if ([dict isKindOfClass:[NSDictionary class]]) {
                        FAQ* f = [FAQ faqFromDictionary:dict];
                        [self.data addObject:f];
                    }
                }
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [Lib removeLoadingView];
        });
    });
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString* cellIdentifier = @"MHFAQCell";
    MHFAQCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
//        cell = [
    }
    
    FAQ* faq = [self.data objectAtIndex:indexPath.row];
    [cell setFaq:faq];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}


@end
